package com.emode.action;
import java.io.PrintWriter;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.emode.base.BaseAction;
import com.emode.entity.Bbssection;
import com.emode.entity.Bbsusers;
import com.emode.entity.SysGroup;
import com.emode.test.logTest;
import com.opensymphony.xwork2.ActionContext;
public class AdminAction extends BaseAction<Bbsusers>{
	 private static Logger logger = Logger.getLogger(AdminAction.class);  

	private static final long serialVersionUID = 1L;
	//显示所有版块 
	public String sectionList(){
		list = sectionservice.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return "sectionlist";
		
	}
	public String toAddsection(){
		//准备回显用户组数据
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);	
		return "addsection";
		
	}
	//添加
	public String addSection(){
		section.setBbsusers(model);
		sectionservice.save(section);;
		return "tosection";
	}
	//修改  准备数据
	public String toEditUI(){
		Bbssection s = sectionservice.getById(section.getSid());
		ActionContext.getContext().put("list", s);
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);	
		return "toedit";
		
	}
	// 保存
	public String update(){
		if("".equals(section.getSname())&&"".equals(section.getSprofile())){
			return null;
			
		}else{
		section.setBbsusers(model);
		sectionservice.update(section);
		//sectionservice.update2(section);
		return "tosection";
		}
	}
	

	//显示所有用户
	public String userList(){
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);
		return "userlist";
		
	}
	//删除
	public String del(){
		System.out.println("---->uid:"+users.getUid());
		userservice.deleteByIds(users.getUid());;
		return "touserlist";
		
	}
	//显示所有的话题
	public String topicList(){
		int count = topicservice.getTopicCount();
		p.init(count);
		list =topicservice.allTopics(p);
		ActionContext.getContext().getValueStack().push(list);
		return "topiclist";
	}
	
	//非法用户关进小黑屋
	public String lockUser() throws Exception{
		ServletActionContext.getResponse().setContentType(
		"text/javascript;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		Bbsusers user = userservice.getById(users.getUid());
		if(0 == user.getUstate()){
			Short state = 4;
			user.setUstate(state);
			userservice.update(user);
			msg = "false";
			out.print(msg);
		}
		else{
			Short state = 0;
			user.setUstate(state);
			userservice.update(user);
			msg = "true";
			out.print(msg);
		}
		
		System.out.println("lock");
		return null;
	}
	//******************************用户组模块*****************//
	//显示所有的用户组
	public String usergroup(){
		List<SysGroup> group = groupservice.findAll();
		ActionContext.getContext().put("group",group);
		return "grouplist";
	}
	//添加用户组
	public String addgroup(){
		groupservice.save(group);
		return usergroup();
	}
	
	//更新用户组
	public String updategroup(){
		groupservice.update(group);
		return usergroup();
	}
	
	// *********************权限模块*************************//
	
}
